import 'package:hotpot/models/create_order_req.dart';
import 'package:hotpot/models/self_ordering_data.dart';
import 'package:hotpot/models/shop_detail.dart';
import 'package:uuid/uuid.dart';

class ProductUtils {
  static int count(List<CreateOrderReqLines> selectLines, ShopDetailProductProducts products) {
    if (products.comboIds?.isNotEmpty ?? false) {
      var list = selectLines
          .where((element) => element.productId == products.id && element.comboLineIds != null && element.hasSend == 0)
          .toList();
      var count = 0;
      for (var item in list) {
        count += item.qty ?? 0;
      }
      return count;
    } else if (products.attributeLineIds?.isNotEmpty ?? false) {
      var list = selectLines
          .where((element) => element.productId == products.id && element.comboParentId == null && element.hasSend == 0);
      return list.map((e) => e.qty ?? 0).fold(0, (previousValue, element) => element + previousValue);
    } else {
      var list = selectLines
          .where((element) => element.productId == products.id && element.comboParentId == null && element.hasSend == 0);
      if (list.isEmpty) {
        return 0;
      } else {
        return list.first.qty ?? 0;
      }
    }
  }

  static void deleteProduct(List<CreateOrderReqLines> selectLines, {ShopDetailProductProducts? products, String? uuid}) {
    if (products != null) {
      if (products.comboIds?.isNotEmpty ?? false) {
        var temp = <CreateOrderReqLines>[];
        for (var item in selectLines.where((element) => element.productId == products.id)) {
          temp.addAll(selectLines.where((element) => item.uuid == element.comboParentId));
        }
        selectLines.removeWhere((element) => temp.contains(element));
        selectLines.removeWhere((element) => element.productId == products.id && element.comboLineIds != null);
      } else {
        selectLines.removeWhere((element) => element.productId == products.id && element.comboParentId == null);
      }
    } else if (uuid != null) {
      selectLines.removeWhere((element) => element.uuid == uuid);
    }
  }

  static void minusProduct(List<CreateOrderReqLines> selectLines, SelfOrderingDataResultProductProducts products) {
    if (products.comboIds?.isNotEmpty ?? false) {
      var temp = <CreateOrderReqLines>[];
      for (var item in selectLines.where((element) => element.productId == products.id)) {
        temp.addAll(selectLines.where((element) => item.uuid == element.comboParentId));
      }
      selectLines.removeWhere((element) => temp.contains(element));
      selectLines.removeWhere((element) => element.productId == products.id && element.comboLineIds != null);
    } else if (products.attributeLineIds?.isNotEmpty ?? false) {
      selectLines.removeWhere((element) => element.productId == products.id && element.comboParentId == null);
    } else {
      var line = selectLines.firstWhere((element) => element.productId == products.id && element.comboParentId == null);
      line.qty = line.qty! - 1;
      line.priceSubtotal = line.qty! * (products.lstPrice ?? 0);
    }
  }

  static void addProduct(List<CreateOrderReqLines> selectLines, SelfOrderingDataResultProductProducts products,
      {void Function()? onAdd, bool isCombo = false}) {
    if (products.comboIds?.isNotEmpty ?? false) {
      // Routes.combo.pushNamed(arg: {'data': products})?.then((value) {
      //   if (value is List<CreateOrderReqLines>) {
      //     selectLines.addAll(value);
      //     onAdd?.call();
      //   }
      // });
    } else if (products.attributeLineIds?.isNotEmpty ?? false) {
      // Routes.attr.pushNamed(arg: {'data': products, 'isCombo': isCombo})?.then((value) {
      //   if (value is CreateOrderReqLines) {
      //     selectLines.add(value);
      //     onAdd?.call();
      //   }
      // });
    } else {
      var lines = selectLines.where((element) => element.productId == products.id && element.comboParentId == null);
      CreateOrderReqLines line;
      if (lines.isEmpty) {
        line = CreateOrderReqLines()
          ..hasSend = 0
          ..qty = 1
          ..priceSubtotal = products.lstPrice ?? 0
          ..priceUnit = products.lstPrice
          ..uuid = const Uuid().v1()
          ..productId = products.id
          ..fullProductName = products.name;
        selectLines.removeWhere((element) => element.productId == products.id && element.comboParentId == null);
        selectLines.add(line);
      } else {
        line = lines.first;
        line.qty = line.qty! + 1;
        line.priceSubtotal = line.qty! * (products.lstPrice ?? 0);
      }
      onAdd?.call();
    }
  }

  static void addProductLine(List<CreateOrderReqLines> selectLines, ShopDetailProductProducts products, int qty) {
    var lines = selectLines.where((element) => element.productId == products.id && element.comboParentId == null);
    CreateOrderReqLines line;
    var isSend = lines.toList().indexWhere((e) => e.hasSend == 0) == -1;
    if (lines.isEmpty || isSend) {
      line = CreateOrderReqLines()
        ..hasSend = 0
        ..qty = qty
        ..priceSubtotal = (products.lstPrice ?? 0) * qty
        ..priceUnit = products.lstPrice
        ..uuid = const Uuid().v1()
        ..productId = products.id
        ..fullProductName = products.name;
      selectLines.removeWhere((element) => element.productId == products.id && element.comboParentId == null);
      selectLines.add(line);
    } else {
      line = lines.first;
      line.qty = line.qty! + qty;
      line.priceSubtotal = line.qty! * (products.lstPrice ?? 0);
    }
  }

  static void addProductAttr(List<CreateOrderReqLines> selectLines, CreateOrderReqLines value) {
    selectLines.add(value);
  }
}
